<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StoreCombination extends Model
{
    protected $guarded = [];
    protected $table = 'store_combination';
    use HasFactory;
    public $timestamps=false;

    /**
     * 查询指定字段
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function storeProduct(){
        return $this->hasOne(StoreProduct::class,'id','product_id')->select(['id','store_name']);
    }

    use HasFactory;
    /**
     * 查询拼团商品
     * @return mixed
     */
    public static function combinationList()
    {
        return self::paginate(3);
    }


    public function GroupGoods($cid)
    {
        return $this->where('id',$cid)->first();
    }


    /**
     * 拼团商品表
     * @return
     */
    public function storeProducts(){
        return $this->hasOne(StoreProduct::class,'id','product_id');
    }


    /**
     * 拼团表
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function storePink(){
        return $this->belongsTo(StorePink::class,'id','cid');
    }

    /**
     * 搜索 分页
     * @param $size
     * @param $is_show
     * @param $title
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public static function storeCombinationWithStoreProductWithUser($size,$is_show,$title){
        $res = self::with(['storeProducts','storePink','storePink.user']);
        if (!empty($is_show)){
            $res->where('is_show',$is_show);
        }
        if (!empty($title)){
            $res->where('title','like',"%$title%");
        }
        return $res->paginate($size);
    }

    /**
     *删除一条数据
     * @param $id
     * @return mixed
     */
    public static function del($id){
        return self::where('id',$id)->delete();
    }

    /**
     * 添加一条数据
     * @param $data
     * @return mixed
     */
    public static function add($data){
        return self::create($data);
    }

    /**
     * 查询一条数据
     * @param $id
     * @return mixed
     */
    public static function find($id){
        return self::with(['storeProducts','storePink','storePink.user'])->where('id',$id)->first();
    }

    /**
     * 修改
     * @param $id
     * @param $data
     * @return mixed
     */
    public static function alter($id,$data){
        return self::where('id',$id)->update($data);
    }

    /**
     * 团长对应的拼团商品
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function recommendGoods(){
        return self::with('headGoods')->where('is_host',1)->get();
    }

    /**
     * 团长拼团商品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function headGoods(){
        return $this->belongsToMany(StoreHead::class,'store_head_goods','hid','cid');
    }
    public function GroupProduct()
    {
        return $this->hasOne(StoreProduct::class,'id','`product_id');
    }
}
